

from datetime import datetime, timedelta
from sqlalchemy import Column, String, Integer, DateTime, func, orm, and_, text
from utils.sql.db import session, Base
from utils.sql.app.record import Man_day

# 查询一周内每天数据量
# 指定查询时间范围
monthList = {
    1: "一月",
    2: "二月",
    3: "三月",
    4: "四月",
    5: "五月",
    6: "六月",
    7: "七月",
    8: "八月",
    9: "九月",
    10: "十月",
    11: "十一月",
    12: "十二月",
}
def test():
    # months = list(range(1, 13))
    # print(months)
    # print(start_date, end_date)
    # 构建查询
    query = (
        session.query(
            # Man_day
            func.month(Man_day.create_time).label("month"),
            func.coalesce(func.sum(Man_day.over_time), 0).label("sum")
        )
        .filter(func.year(Man_day.create_time) == 2024, func.month(Man_day.create_time) >= 1, Man_day.user_id == 'ouWAN0S26fQ85AEDweRy_NDNf58M')
        .group_by(func.month(Man_day.create_time))
    )

    # 执行查询并获取结果
    # results = query.all()
    date_dict = {date[0]: date[1] for date in query}
    for result in range(1, 13):
        data = date_dict.get(result, 0)

        print(monthList[result], '-', data, '/', date_dict)

        # print(f"{date}: {data}")
    # 关闭会话
    session.close()
    # return date_dict
    # return {
    #     "data": "test"
    # }
test()